<?php
//供应商
namespace Common\Model;

class UserKuerpWarehouseLocationModel extends BaseModel {

	//追加属性
    public $append = [
        'add_time_text',    //添加时间
        'edit_time_text',   //编辑时间
        'wh_name',          //仓库名称
        'product_num',      //商品总数
        'stock_num',        //库存总数量
	];

    /**
     * 类型
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getAtypeAttr($value, $data){
        $kw_model = M('UserKuerpWarehouse');
        $atype = $kw_model->where('id='.$data['wh_id'])->getField('atype');
		return $atype;
    }

    /**
     * 库存总数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStockNumAttr($value, $data){
        $num = 0;
        $model = M('UserKuerpWarehouseLocProduct');
        $wdata['loc_id'] = $data['id'];
        $num = $model->where($wdata)->sum('stock') ? : 0;
		return $num;
    }

    /**
     * 商品总数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getProductNumAttr($value, $data){
        $num = 0;
        $model = M('UserKuerpWarehouseLocProduct');
        $wdata['loc_id'] = $data['id'];
        $num = $model->where($wdata)->distinct('product_id')->count() ? : 0;
		return $num;
    }

    /**
     * 仓库信息
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getWhNameAttr($value, &$data){
        $name = '';
		$model = D('UserKuerpWarehouse');
		$wdata['id'] = $data['wh_id'];
		//$name = $model->where($wdata)->getField('name') ? : '';
        $wh_vo = $model->where($wdata)->find();
        $name = $wh_vo['name'];
        $data['type_name'] = $wh_vo['type_name'];
		return $name;
    }


    //删除
    function fdel($id){
        $return = [
            'status' => false,
            'errcode'   => 10000,
            'data'   => '',
            'msg'    => '失败'
        ];
        $sqls = [];
        $kwl_wdata['id'] = $id;
        $kwl_vo = $this->where($kwlp_wdata)->find();
        if(!$kwl_vo){
            $return['msg'] = '仓位不存在 - 1001';
            return $return;
        }
        $kw_model = D('UserKuerpWarehouse');
        $kw_wdata['id'] = $kwl_vo['wh_id'];
        $kw_vo = $kw_model->where($kw_wdata)->find();
        $kwlp_model = M('UserKuerpWarehouseLocProduct');
        $kwlp_wdata['loc_id'] = $id;
        $stock = $kwlp_model->where($kwlp_wdata)->sum('stock') ? : 0;
        if($stock>0){
            $return['msg'] = '仓位有商品不能删除 - 1002';
            return $return;
        }
        //删除仓位
        $res = $this->where($kwl_wdata)->delete();
        if($res===false){
            $return['msg'] = '删除仓位出错 - 1003';
            return $return;
        }
        $sqls[] = M()->_sql();
        //仓库减少仓位
        $kw_model = D('UserKuerpWarehouse');
        $res = $kw_model->tjkw($kwl_vo['wh_id']);
        $sqls[] = M()->_sql();
        $return['status'] = true;
        //$return['data'] = $sqls;
        $return['msg'] = '删除成功';
        return $return;

    }

    //搜索
    function _before_search(){
        $map = [];
        if(I('keyword')){
            $map['name'] = array('like','%'.I('keyword').'%');
        }
        if(I('atype')){
            $model = M('UserKuerpWarehouse');
            $wdata['atype'] = I('atype');
            $wdata['is_delete'] = 0;
            $list = $model->field('id')->where($wdata)->select();
            foreach($list as $val){
                $wh_ids[] = $val['id'];
            }
            if($wh_ids){
                $map['wh_id'] = array('in',$wh_ids);
            }
           
        }
        return $map;
    }

   
}

?>